package order

import (
	"time"

	"gitcode.com/G-YT/iHealth25/server/global"
	"gitcode.com/G-YT/iHealth25/server/model/user"
)

// Order 订单结构体
type Order struct {
	global.GVA_MODEL
	OrderNo        *string    `json:"orderNo" form:"orderNo" gorm:"comment:订单编号;column:order_no;size:50;uniqueIndex;" binding:"required"`      //订单编号
	OutTradeNo     string     `json:"outTradeNo" form:"outTradeNo" gorm:"comment:商户订单号;column:out_trade_no;size:50;uniqueIndex;"`             //商户订单号
	TransactionId  *string    `json:"transactionId" form:"transactionId" gorm:"comment:微信支付订单号;column:transaction_id;size:50;"`             //微信支付订单号
	UserId         uint       `json:"userId" form:"userId" gorm:"comment:用户ID;column:user_id;" binding:"required"`                               //用户ID
	User           user.User  `json:"user" gorm:"foreignKey:UserId;references:ID"`                                                                 //关联用户信息
	ProductId      *int       `json:"productId" form:"productId" gorm:"comment:商品ID;column:product_id;" binding:"required"`                      //商品ID
	ProductName    *string    `json:"productName" form:"productName" gorm:"comment:商品名称;column:product_name;size:200;" binding:"required"`     //商品名称
	ProductType    uint       `json:"productType" form:"productType" gorm:"comment:商品类型;column:product_type;default:1;"`                       //商品类型 1:实体商品 2:虚拟商品
	ProductSku     *string    `json:"productSku" form:"productSku" gorm:"comment:商品SKU;column:product_sku;size:100;"`                            //商品SKU
	SelectedSpecs  *string    `json:"selectedSpecs" form:"selectedSpecs" gorm:"comment:用户选择的规格;column:selected_specs;type:text;"`           //用户选择的规格
	UnitPrice      *float64   `json:"unitPrice" form:"unitPrice" gorm:"comment:单价;column:unit_price;type:decimal(10,2);" binding:"required"`     //单价
	Quantity       uint       `json:"quantity" form:"quantity" gorm:"comment:数量;column:quantity;" binding:"required"`                            //数量
	Amount         *float64   `json:"amount" form:"amount" gorm:"comment:订单金额;column:amount;type:decimal(10,2);" binding:"required"`           //订单金额
	Status         uint       `json:"status" form:"status" gorm:"comment:订单状态;column:status;default:1;"`                                       //订单状态 1:待支付 2:已支付 3:已取消 4:已退款
	PaymentType    uint       `json:"paymentType" form:"paymentType" gorm:"comment:支付类型;column:payment_type;default:1;"`                       //支付类型 1:微信支付 2:支付宝 3:云闪付
	PayTime        *time.Time `json:"payTime" form:"payTime" gorm:"comment:支付时间;column:pay_time;"`                                             //支付时间
	TradeState     int        `json:"tradeState" form:"tradeState" gorm:"comment:微信支付状态;column:trade_state;default:1;"` //微信支付状态 1:未支付 2:支付成功 3:转入退款 4:已关闭 5:已撤销 6:用户支付中 7:支付失败
	TradeStateDesc string     `json:"tradeStateDesc" form:"tradeStateDesc" gorm:"comment:微信支付状态描述;column:trade_state_desc;size:100;"`      //微信支付状态描述
	TradeType      string     `json:"tradeType" form:"tradeType" gorm:"comment:交易类型;column:trade_type;size:20;"`                               //交易类型 JSAPI:公众号支付 NATIVE:扫码支付 APP:APP支付
	Remark         *string    `json:"remark" form:"remark" gorm:"comment:备注;column:remark;type:text;"`                                           //备注
	Openid         string     `json:"openid" form:"openid" gorm:"comment:微信用户标识;column:openid;size:50;"`                                     //微信用户标识
	AppID          string     `json:"appID" form:"appID" gorm:"comment:应用ID;column:appID;size:50;"`                                              //应用ID
	MchID          string     `json:"mchID" form:"mchID" gorm:"comment:商户号;column:mchID;size:50;"`                                              //商户号
	Total          int        `json:"total" form:"total" gorm:"comment:订单金额(分);column:total;"`                                                //订单金额(分)
	PayerTotal     int        `json:"payerTotal" form:"payerTotal" gorm:"comment:用户支付金额(分);column:payer_total;"`                            //用户支付金额(分)
	Currency       string     `json:"currency" form:"currency" gorm:"comment:货币类型;column:currency;size:10;default:'CNY';"`                     //货币类型
	PayerCurrency  string     `json:"payerCurrency" form:"payerCurrency" gorm:"comment:用户支付币种;column:payer_currency;size:10;default:'CNY';"` //用户支付币种
	Attach         string     `json:"attach" form:"attach" gorm:"comment:附加数据;column:attach;size:200;"`                                        //附加数据
	BankType       string     `json:"bankType" form:"bankType" gorm:"comment:银行类型;column:bank_type;size:50;"`                                  //银行类型
	SuccessTime    *time.Time `json:"successTime" form:"successTime" gorm:"comment:支付完成时间;column:success_time;"`                             //支付完成时间
}

// TableName 订单表 Order自定义表名 ih_orders
func (Order) TableName() string {
	return "ih_orders"
}

// OrderStatus 订单状态常量
const (
	OrderStatusPending   uint = 1 // 待支付
	OrderStatusPaid      uint = 2 // 已支付
	OrderStatusCancelled uint = 3 // 已取消
	OrderStatusRefunded  uint = 4 // 已退款
)

// PaymentType 支付类型常量
const (
	PaymentTypeWechat   uint = 1 // 微信支付
	PaymentTypeAlipay   uint = 2 // 支付宝
	PaymentTypeUnionpay uint = 3 // 云闪付
)

// ProductType 商品类型常量
const (
	ProductTypePhysical uint = 1 // 实体商品
	ProductTypeVirtual  uint = 2 // 虚拟商品
)

// TradeState 微信支付状态常量
const (
	TradeStateNotPay     int = 1 // 未支付
	TradeStateSuccess    int = 2 // 支付成功
	TradeStateRefund     int = 3 // 转入退款
	TradeStateClosed     int = 4 // 已关闭
	TradeStateRevoked    int = 5 // 已撤销
	TradeStateUserPaying int = 6 // 用户支付中
	TradeStatePayError   int = 7 // 支付失败
)

// TradeStateStringMap 微信支付状态字符串映射
var TradeStateStringMap = map[string]int{
	"NOTPAY":     TradeStateNotPay,
	"SUCCESS":    TradeStateSuccess,
	"REFUND":     TradeStateRefund,
	"CLOSED":     TradeStateClosed,
	"REVOKED":    TradeStateRevoked,
	"USERPAYING": TradeStateUserPaying,
	"PAYERROR":   TradeStatePayError,
}

// TradeStateIntMap 微信支付状态整数映射
var TradeStateIntMap = map[int]string{
	TradeStateNotPay:     "NOTPAY",
	TradeStateSuccess:    "SUCCESS",
	TradeStateRefund:     "REFUND",
	TradeStateClosed:     "CLOSED",
	TradeStateRevoked:    "REVOKED",
	TradeStateUserPaying: "USERPAYING",
	TradeStatePayError:   "PAYERROR",
}
